package map_set.leetcode;

//138. 复制带随机指针的链表

import java.util.HashMap;
import java.util.Map;

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Num138 {
    public Node copyRandomList(Node head) {
        if(head == null){
            return null;
        }

        Map<Node,Node> map = new HashMap<>();
        for(Node x = head;x != null;x = x.next){
            Node newNode = new Node(x.val);
            map.put(x,newNode);
        }

        for(Node x = head; x != null;x = x.next){
            //next
            map.get(x).next = map.get(x.next);
            //random
            map.get(x).random = map.get(x.random);
        }

        return map.get(head);
    }
}
